package com.anis.project.medcare.ooswriter;

import java.awt.Panel;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

import javax.print.PrintService;
import javax.print.PrintServiceLookup;

import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.osgi.framework.Bundle;

import com.sun.star.beans.PropertyValue;
import com.sun.star.comp.beans.NoConnectionException;
import com.sun.star.comp.beans.NoDocumentException;
import com.sun.star.comp.beans.OOoBean;
import com.sun.star.comp.beans.OfficeDocument;
import com.sun.star.comp.beans.SystemWindowException;
import com.sun.star.frame.XDesktop;
import com.sun.star.lang.IllegalArgumentException;
import com.sun.star.util.CloseVetoException;

public class OOoSwtViewer extends Panel {

	private static final String OFFICE_DOCUMENT = "file:///home/snpe/ThatIsLife.doc";

	public final String SWRITER = "private:factory/swriter";

	private static final long serialVersionUID = -1408623115735065822L;

	private OOoBean aBean;

	URL fileURL;

	public OOoSwtViewer() {
		super();
		aBean = new OOoBean();
		aBean.setMenuBarVisible(false);
		setLayout(new java.awt.BorderLayout());
		add(aBean, java.awt.BorderLayout.CENTER);
	}

	public void setDocument(String url) throws IllegalArgumentException,
			CloseVetoException, NoConnectionException, IOException,
			SystemWindowException {
		aBean.loadFromURL(url, null);
		aBean.aquireSystemWindow();
	}

	/**
	 * closes the bean viewer and tries to terminate OOo.
	 */
	public void terminate() throws NoConnectionException {
		setVisible(false);
		XDesktop xDesktop = null;
		xDesktop = aBean.getOOoDesktop();
		aBean.stopOOoConnection();
		if (xDesktop != null)
			xDesktop.terminate();
	}

	/**
	 * closes the bean viewer, leaves OOo running.
	 */
	public void close() {
		setVisible(false);
		aBean.stopOOoConnection();
	}

	public OfficeDocument getDocument() {
		OfficeDocument document = null;
		try {
			document = aBean.getDocument();
		} catch (NoConnectionException e) {
			e.printStackTrace();
		}
		return document;
	}

	public void setDocumentBinary(byte[] officeDocument) {
		try {
			aBean.loadFromByteArray(officeDocument, null);
		} catch (IllegalArgumentException | CloseVetoException
				| NoConnectionException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public byte[] storeDocumentToByteArray() {
		byte[] documentToReturn = null;
		try {
			documentToReturn = this.aBean.storeToByteArray(null, null);
		} catch (IllegalArgumentException | NoConnectionException
				| NoDocumentException | IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return documentToReturn;
	}

	public void printDocument() {
		try {
			PrintService service = PrintServiceLookup
					.lookupDefaultPrintService();
			String printerName = service.getName();
			PropertyValue propertyValue[] = new PropertyValue[1];
			propertyValue[0] = new PropertyValue();
			propertyValue[0].Name = printerName;
			aBean.getDocument().print(propertyValue);
		} catch (IllegalArgumentException | NoConnectionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}